package com.mark.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.rmi.ConnectException;

/**
 * @author Mark Liang
 **/
public class UrlConnection {
	
	private InputStream netGetInputStream;
		
	public UrlConnection(String proxyIp ,int Port,String urlString){
		try{
			InetSocketAddress socketAddress=new InetSocketAddress( 
					InetAddress.getByName(proxyIp),Port); 
		
			Proxy proxy=new Proxy(Proxy.Type.HTTP,socketAddress); 
			
			URL url= new URL(urlString);
			InputStream input=url.openConnection(proxy).getInputStream(); 
			if(input !=null){  
	            //System.out.println("Connectioned");  
	            //System.out.println("Input " +Common.inputStream2String(input) ); 
	            netGetInputStream = input;
	        }  
		}catch(MalformedURLException e){
			e.printStackTrace();
			//System.out.println("Test1");
		}catch(ConnectException e){
			e.printStackTrace();
			//System.out.println("Test2");
		}catch(IOException e){
			e.printStackTrace();
			//System.out.println("Test3");
		}catch(Exception e){
			e.printStackTrace();
			//System.out.println("Test4");
		}
	}
	
	public UrlConnection(String urlString){
		try{
			URL url= new URL(urlString);
			InputStream input=url.openConnection().getInputStream(); 
			if(input != null){
				netGetInputStream = input;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public InputStream getReturnStream(){
		return netGetInputStream;
	}

}
